﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmPersonalInformation : Form
    {
        public frmPersonalInformation()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();

        private void frmPersonalInformation_Load(object sender, EventArgs e)
        {
            try
            {
                dtpBirthday.MaxDate = DateTime.Today;
                string uname = Common.username[0].ToString();
                string sql = "SELECT USER_INFORMATION.* FROM USER_INFORMATION WHERE USER_NAME='" + uname + "'";
                SqlConnection con = Common.con;
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataAdapter adap = new SqlDataAdapter(com);
                adap.Fill(dt);
                con.Close();
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("This account not yet update information. Please wait!");
                    txtUsername.Text = uname;
                }

                else
                {
                    string gt;
                    txtUsername.Text = dt.Rows[0][0].ToString();
                    txtFullname.Text = dt.Rows[0][1].ToString();
                    txtAddress.Text = dt.Rows[0][3].ToString();
                    dtpBirthday.Value = Convert.ToDateTime(dt.Rows[0][4]);
                    txtPhone.Text = dt.Rows[0][5].ToString();
                    if (dt.Rows[0][6].ToString() == "True") gt = "Male";
                    else gt = "Female";
                    cbxGender.Text = gt;
                }
            }
            catch { }
        }        

        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtFullname.Enabled = true;
            txtAddress.Enabled = true;
            dtpBirthday.Enabled = true;
            txtPhone.Enabled = true;
            cbxGender.Enabled = true;
            btnEdit.Visible = false;
            btnSave.Visible = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string phone = txtPhone.Text;
                DateTime birthday = Convert.ToDateTime(dtpBirthday.Value);
                string today = DateTime.Today.Year.ToString();
                string birthdaynum = birthday.Year.ToString();
                if (int.Parse(today) - int.Parse(birthdaynum) < 18)
                {
                    MessageBox.Show("Birthday must be greater than 18 years.");
                }
                else
                {
                    string gender = checkgener();
                    string sql = "UPDATE USER_INFORMATION SET FULL_NAME='" + txtFullname.Text + "',ADDRESS='" + txtAddress.Text + "',BIRTHDAY='" + birthday + "',PHONE=" + phone + ",GENDER='" + gender + "' WHERE USER_NAME='" + txtUsername.Text + "'";
                    SqlConnection con = Common.con;
                    con.Open();
                    SqlCommand com = new SqlCommand(sql, con);
                    int kq = com.ExecuteNonQuery();
                    con.Close();
                    if (kq > 0)
                    {
                        MessageBox.Show("Edit success");
                        this.Close();
                    }
                    else
                        MessageBox.Show("Fail to edit");
                    txtFullname.Enabled = false;
                    txtAddress.Enabled = false;
                    dtpBirthday.Enabled = false;
                    txtPhone.Enabled = false;
                    cbxGender.Enabled = false;
                    btnEdit.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string checkgener()
        {
            string gender;
            if (cbxGender.SelectedText=="Male") gender = "False";
            gender = "True";
            return gender;
        }
    }
}
